//
//  String+Ex.swift
//  APP
//
//  Created by Rdxer on 2017/12/14.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import Foundation

/// from https://qa.1r1g.com/sf/ask/1696065381/
public extension Array  where Element == Int{
    func toHex(full_0:Bool = true,len:Int=2,lowerCase:Bool=true,divider:String = "")->String{
        
        var str = ""
        
        for v in self {
            str += v.toHex(full_0: full_0, len: len, lowerCase: lowerCase)
            str += divider
            
        }
        
        return str
        
    }
}

public extension String {
    public func hexListToUInt8List(_ errCallback:(Int,String)->(UInt8))->[UInt8]{
        var comp:[String] = []
        if contains(","){
            comp = split(separator: ",").map({$0.description})
        }else if contains(" "){
            comp = split(separator: " ").map({$0.description})
        }else if length % 2 == 0{
            for index in 0..<(length/2) {
                let i1 = index * 2
//                let i2 = i1 + 1
                let str = asNSString.substring(with: NSRange.init(location: i1, length: 2))
                comp.append(str)
            }
        }else{
            XXLoger.e("hexstr to uint8 fail!! > " + self)
        }
        
        
//        var cmdU8List = comp.toUInt8List({ index, v in
//            if var paraIndex = cmd.paraIndex{
//                if paraIndex.index(forKey: v.trim) == nil{
//                    paraIndex[v.trim] = index
//                    cmd.paraIndex = paraIndex
//                }
//            }
//            return 0
//        })
        return comp.toUInt8List(errCallback)
    }
}

public extension Int{
    func toHex(full_0:Bool = true,len:Int=2,lowerCase:Bool=true) -> String {
        
        var formatstr = ""
        
        formatstr.append("%")
        
        if full_0 {
            formatstr.append("0")
            
        }
        formatstr.append(len.description)
        
        if lowerCase {
            formatstr.append("x")
        }else{
            formatstr.append("X")
        }
        
        var st = String(format:formatstr, self)
        
        return st;
    }
    /// 转成字符串 十进制
    func toString(full_0:Bool = true,len:Int=2) -> String {
        
        var formatstr = ""
        
        formatstr.append("%")
        
        if full_0 {
            formatstr.append("0")
        }
        formatstr.append(len.description)
        
//        if lowerCase {
            formatstr.append("d")
//        }else{
//            formatstr.append("X")
//        }
        
        var st = String(format:formatstr, self)
        
        return st;
    }
    /// len: 最小长度
    func toRadix(radix:Int,full_0:Bool = true,len:Int=2) -> String {
        
        var st = String(self,radix: radix)
        
        if full_0 {
            
            if st.count >= len{
                return st
            }else{
                return st.full_lead(len: len, full: "0")
            }
        }
        
        return st;
    }
}
public extension String{
    /// 填充到开头
    func full_lead(len:Int,full:String,cut:Bool = false) -> String {
        if self.count >= len {
            if cut {
                return self.asNSString.substring(with: NSRange.init(location: self.count-len, length: len))
            }
            return self
        }
        let needFull = len - self.count
        var result = self
        for i in 0..<needFull {
            result = full + result
        }
        return result
    }
}

public extension String{
    func hexToInt() -> Int? {
        let v1 = self.trim
        let v2 = v1.asNSString.replacingOccurrences(of: "0x", with: "")
        
        return Int.init(v2, radix: 16) ?? nil
    }
    
    
    func toInt() -> Int? {
        return Int.init(self) ?? nil
    }
    
}
